package daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import connector.Connector;
import daointerfaces.DALException;
import daointerfaces.OperatoerDAO;
import dto.OperatoerDTO;

public class MySQLOperatoerDAO implements OperatoerDAO {
	public OperatoerDTO getOperatoer(int oprId) throws DALException {
		System.out.println("Trying to get Operator");
		ResultSet rs = Connector.doQuery("SELECT * FROM operatoerCDIO WHERE oprId = " + oprId);
		try {
			if (!rs.first()) throw new DALException("Operatoeren " + oprId + " findes ikke");
			return new OperatoerDTO (rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),rs.getString(6));
		}
		catch (SQLException e) {throw new DALException(e); }
	}

	public void createOperatoer(OperatoerDTO opr) throws DALException {
		Connector.doUpdate(
				"INSERT INTO operatoerCDIO(oprId, oprNavn, ini, cpr, password, rights) VALUES " +
						"(" + opr.getOprId() + ", '" + opr.getOprNavn() + "', '" + opr.getIni() + "', '" + 
						opr.getCpr() + "', '" + opr.getPassword() + "', '" + opr.getRights() + "')"
				);
	}

	public void deleteOperatoer(int oprId) throws DALException {
		Connector.doUpdate(
				"DELETE FROM operatoerCDIO WHERE oprId=" + oprId
				);
	}

	public void updateOperatoer(OperatoerDTO opr) throws DALException {
		Connector.doUpdate(
				"UPDATE operatoerCDIO SET  oprNavn = '" + opr.getOprNavn() + "', ini =  '" + opr.getIni() + 
				"', cpr = '" + opr.getCpr() + "', password = '" + opr.getPassword() + "', rights = '" + opr.getRights() + "' WHERE oprId = " +
				opr.getOprId()
				);
	}

	public List<OperatoerDTO> getOperatoerList() throws DALException {
		List<OperatoerDTO> list = new ArrayList<OperatoerDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM operatoerCDIO");
		try {
			while (rs.next())
			{
				list.add(new OperatoerDTO(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	public List<OperatoerDTO> getOperatoerListNr(int oprId) throws DALException {
		List<OperatoerDTO> list = new ArrayList<OperatoerDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM operatoerCDIO WHERE oprId = "+oprId);
		try
		{
			while (rs.next())
			{
				list.add(new OperatoerDTO(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	public List<OperatoerDTO> getOperatoerListGroup() throws DALException {
		List<OperatoerDTO> list = new ArrayList<OperatoerDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM operatoerCDIO GROUP BY oprId");
		try
		{
			while (rs.next())
			{
				list.add(new OperatoerDTO(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}
}